﻿using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;

namespace Utils
{
    public class HttpHelper
    {

        private readonly HttpClient httpClient;
        public HttpHelper(HttpClient _httpClient)
        {
            httpClient = _httpClient;
        }

        // Post请求
        public async Task<string> PostResponse(string url, string postData)
        {
            string result = string.Empty;
            //设置Http的正文
            HttpContent httpContent = new StringContent(postData);
            //设置Http的内容标头
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")
            {
                //设置Http的内容标头的字符
                CharSet = "utf-8"
            };

            //异步Post
            HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);
            //输出Http响应状态码
            //statusCode = response.StatusCode.ToString();
            //确保Http响应成功
            if (response.IsSuccessStatusCode)
            {
                //异步读取json
                result = await response.Content.ReadAsStringAsync();
            }
            return result;
        }

        // 泛型：Post请求
        public async Task<T> PostResponse<T>(string url, string postData) where T : class, new()
        {
            T result = default(T);

            HttpContent httpContent = new StringContent(postData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";

            HttpResponseMessage response = await httpClient.PostAsync(url, httpContent);

            if (response.IsSuccessStatusCode)
            {
                string s = await response.Content.ReadAsStringAsync();
                //Newtonsoft.Json
                string json = JsonConvert.DeserializeObject(s).ToString();
                result = JsonConvert.DeserializeObject<T>(json);
            }

            return result;
        }

        // Get请求
        public async Task<string> GetResponse(string url)
        {
            string result = string.Empty;

            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await httpClient.GetAsync(url);
            //statusCode = response.StatusCode.ToString();

            if (response.IsSuccessStatusCode)
            {
                result = await response.Content.ReadAsStringAsync();
            }

            return result;
        }

        // 泛型：Get请求
        public async Task<T> GetResponse<T>(string url) where T : class, new()
        {
            T result = default(T);

            httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await httpClient.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                string s = await response.Content.ReadAsStringAsync();
                string json = JsonConvert.DeserializeObject(s).ToString();
                result = JsonConvert.DeserializeObject<T>(json);
            }

            return result;
        }

        // Put请求
        public async Task<string> PutResponse(string url, string putData)
        {
            string result = string.Empty;
            HttpContent httpContent = new StringContent(putData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";

            HttpResponseMessage response = await httpClient.PutAsync(url, httpContent);
            if (response.IsSuccessStatusCode)
            {
                result = await response.Content.ReadAsStringAsync();
            }

            return result;
        }

        // 泛型：Put请求
        public async Task<T> PutResponse<T>(string url, string putData) where T : class, new()
        {
            T result = default(T);
            HttpContent httpContent = new StringContent(putData);
            httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";

            HttpResponseMessage response = await httpClient.PutAsync(url, httpContent);

            if (response.IsSuccessStatusCode)
            {
                string s = await response.Content.ReadAsStringAsync();
                string json = JsonConvert.DeserializeObject(s).ToString();
                result = JsonConvert.DeserializeObject<T>(json);
            }

            return result;
        }
    }
}
